package com.hk.webapp.filter;

import lombok.extern.slf4j.Slf4j;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * (C) Copyright 2017-now
 * All rights reserved.
 * <p>
 * 全局跨域设置
 *
 * @author DreamerCK
 * @date 2019-01-08 11:30
 **/
@Slf4j
@WebFilter(filterName = "corsFilter", urlPatterns = "/*")
public class CorsFilter implements Filter {

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {

    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        HttpServletResponse res = (HttpServletResponse) servletResponse;
        res.setHeader("Access-Control-Allow-Origin", "*");
        res.setHeader("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE,OPTIONS");
        res.setHeader("Access-Control-Max-Age", "3600");
        res.setHeader("Access-Control-Allow-Headers", "content-type,application/x-www-form-urlencoded,application/json,x-requested-with,Authorization");
        res.setHeader("Access-Control-Allow-Credentials", "true");
        filterChain.doFilter(servletRequest, res);
    }

    @Override
    public void destroy() {

    }
}

